<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT">





  <link rel="alternate" href="/atom.xml" title="renhao" type="application/atom+xml">






<meta name="description" content="本文讨论列表布局的实现方式以及列表中如何实现两端对齐的做法。">
<meta property="og:type" content="article">
<meta property="og:title" content="display:inline-block与列表布局">
<meta property="og:url" content="https://huangrenhao.gitee.io/2019/09/14/css-text-align/index.html">
<meta property="og:site_name" content="renhao">
<meta property="og:description" content="本文讨论列表布局的实现方式以及列表中如何实现两端对齐的做法。">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2020-09-03T08:50:36.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="display:inline-block与列表布局">
<meta name="twitter:description" content="本文讨论列表布局的实现方式以及列表中如何实现两端对齐的做法。">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://huangrenhao.gitee.io/2019/09/14/css-text-align/">





  <title>display:inline-block与列表布局 | renhao</title>
  








<link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css"></head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-Hans">
  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">renhao</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://huangrenhao.gitee.io/2019/09/14/css-text-align/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="renhao">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="renhao">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">display:inline-block与列表布局</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-09-14T09:24:56+08:00">
                2019-09-14
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/categories/css/" itemprop="url" rel="index">
                    <span itemprop="name">css</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/09/14/css-text-align/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2019/09/14/css-text-align/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  1.2k
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  4
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>本文讨论列表布局的实现方式以及列表中如何实现两端对齐的做法。<a id="more"></a></p>
<h3 id="列表布局"><a href="#列表布局" class="headerlink" title="列表布局"></a>列表布局</h3><p>列表浮动布局：通过使用float属性让列表元素依次排列。局限性在于每个列表元素高度必须一致，否则列表元素会错开，同时还有浮动带来的高度塌陷问题。</p>
<p>表格布局：表格中的单元格<code>&lt;td&gt;</code>会自动等高关联且水平列表项还包裹在<code>&lt;tr&gt;</code>中（不会错位），但表格布局局限于IE6/7对这些<code>display</code>属性不支持（<code>display:table;display:table-row;display:table-cell;</code>）。</p>
<p><code>display:line-block</code>，没有父元素的匿名包裹特性，使用非常自由，可与文字、图片混排，可内嵌<code>block</code>属性或置于<code>inline</code>水平元素中。IE6/7不支持<code>display:inline-block</code>属性，只是让其表现的跟<code>inline-block</code>一样。故对于IE8+以及现代浏览器直接使用<code>display:inline-block</code>；对于IE6/7浏览器，如果是<code>inline</code>水平元素使用<code>display:inline-block</code>，如果是<code>block</code>水平元素（如<code>li</code>）使用<code>li{display:inline-block;} li{display:inline;}</code>或者<code>li{display:inline;zoom:1;}</code>。inline-block元素间可能会出现换行符空格间隙，解决这个问题的方法只需要使用<code>font-size:0</code>消除换行间隙（空格符本质是字符），或者使用<code>letter-spacing</code>（控制文字间水平距离），将<code>letter-spacing</code>设置为负值就可是文字水平方向上重叠同时不会使元素发生水平重叠（元素本身没有空格）。</p>
<p>display:inline-block实现表格布局：相比于浮动布局可以不用考虑每个列表高度不一的问题（需要注意换行符空格间隙）。</p>
<h3 id="display-inline-block-text-align-justify下列表的两端对齐布局"><a href="#display-inline-block-text-align-justify下列表的两端对齐布局" class="headerlink" title="display:inline-block/text-align:justify下列表的两端对齐布局"></a>display:inline-block/text-align:justify下列表的两端对齐布局</h3><p>“列表元素”（指具有类似结构的重复列表元素）的两端对齐就是每行列表元素的第一个元素和父容器左边缘重合，最后一个元素与父容器的右边缘重合。</p>
<p><code>text-align</code>主要用来控制inline水平元素或inline-block元素的对齐与显示（如嵌套行内标签的文字、图片、input表单控件等），对block水平元素没有作用。<code>text-align</code>有一个属性值为<code>justify</code>，实现效果可以让一行文字两端对齐显示（文字内容要超过一行）。</p>
<p>列表元素实现两端对齐：</p>
<ul>
<li>浮动，通过<code>width</code>属性强行增大父容器的宽度实现看上去的“两端对齐”效果。</li>
<li>inline-block布局，通过使用margin负值来增大父标签的宽度实现看上去的“两端对齐”效果。</li>
<li><code>white-space:nowrap</code>，会强制列表元素不换行，可以无需设定父标签容器的宽度或者通过<code>margin</code>负值等手段增加父容器的宽度等。需要在inline-block布局基础上使用，</li>
</ul>
<p>用<code>text-align:justify</code>实现两端对齐比较简单方便，只要一个简单的声明里面的元素就自动等间距两端对齐布局，无需计算每个列表元素间的margin间距或者修改父容器的宽度。</p>
<p>对于IE8+浏览器及现代浏览器：以<code>ul</code>、<code>li</code>为例：<code>ul{text-align:justify;}li{display:inline-block;}</code>即可实现列表元素的两端对齐。需要注意相应列表元素标签之间要留空或者换行（不能连着一起），并且列表元素不能处于<code>font-size:0</code>的环境下（因为IE8浏览器<code>font-size:0</code>会直接把换行空格或普通空格抹掉而无法实现两端对齐）。</p>
<p>IE6/7浏览器：列表元素需要使用inline水平的标签（如<code>span,a,strong,em</code>，不能用<code>li,div</code>），列表元素及其内部标签的结束标签需要连在一起。如果列表标签内嵌多层则所有层级的结束标签都要连续，同样首尾标签之间需要留空。最后一行元素可能无法实现两端对齐（内容没有超过一行），可以在列表（或文字段）的最后创建一个高度为0的宽度100%的透明的inline-block标签层，如果要实现左对齐的话可以在最后一行添加若干个所需的列表元素占位，等宽但高度为0，里面是<code>&amp;nbsp</code>，并且需要<a href="https://www.zhangxinxu.com/wordpress/2015/08/css-deep-understand-vertical-align-and-line-height/" target="_blank" rel="noopener">修正空白空间</a>。</p>
<blockquote>
<p>空白空间产生的原因主要是inline-block与baseline之间的关系：一个inline-block元素如果里面没有inline内联元素，或者overflow不是visible，则该元素的基线就是其margin底边缘，否则其基线就是元素里面最后一行内联元素的基线。可以通过改正占位空白元素的基线位置、改正空白节点（假想）的基线位置、使用其他的vertical-align对齐方式来解决。</p>
</blockquote>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/09/13/css-float/" rel="next" title="浮动">
                <i class="fa fa-chevron-left"></i> 浮动
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/09/19/css-no-external-link/" rel="prev" title="CSS无外链的开发策略">
                CSS无外链的开发策略 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">renhao</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">94</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">19</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#列表布局"><span class="nav-number">1.</span> <span class="nav-text">列表布局</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#display-inline-block-text-align-justify下列表的两端对齐布局"><span class="nav-number">2.</span> <span class="nav-text">display:inline-block/text-align:justify下列表的两端对齐布局</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">renhao</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">Site words total count&#58;</span>
    
    <span title="Site words total count">265.9k</span>
  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: false,
        appId: '2P1xqUWiCoNm14MH4yhURlJi-gzGzoHsz',
        appKey: 'uJN0uagHIVgXtBO1OuLV9Ban',
        placeholder: 'Just go go',
        avatar:'mm',
        guest_info:guest,
        pageSize:'10' || 10,
    });
  </script>



  





  

  

  

  
  

  

  

  

</body>
</html>
